package com.xialing.component;

import com.xialing.util.CommonUtil;
import org.springframework.stereotype.Component;

/**
 * @Author: xialing
 * @Description
 */
@Component
public class ShortLinkComponent {

    private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 创建短链码
     * @param param
     * @return
     */
    public String createShortLinkCode(String param) {

        long murmurHash = CommonUtil.murmurHash32(param);
        // 进制转换
        String code = encodeToBase62(murmurHash);

        return code;
    }



    /**
     * 62进制转换
     * @param number
     * @return
     */
    private String encodeToBase62(long number) {

        StringBuffer sb = new StringBuffer();

         do {

             int i = (int) (number % 62);
             sb.append(CHARS.charAt(i));
             number = number / 62;

         } while (number > 0);

         String value = sb.reverse().toString();
         return value;

    }

}
